var app = getApp()
Page({
  data: {
    hasData:false,
    total_mileage: 0,
    total_duration: 0,
    shop_number: 0,
    al_shop_number: 0,
    shop_data: [],
    delivery_data: []
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": app.appName +"--配送详情"});
    var that = this;
    wx.showToast({
      title: "数据加载中，请稍候...",
      icon: 'loading',
      duration: 3000
    });
    wx.request({
      url: app.appName +'/wechat/wxapplet/getCurrentOrderData',
        method:'GET',
        header: {
              'content-type': 'application/json',
              'Cookie':app.globalData.cookie
        },
        data: {
          id:e.id
        },
        success: function(res) {
          wx.hideToast();
          if(res.data.success){
            console.log(res.data.data)
            that.setData({
              hasData:true
            });
            var delivery_data = res.data.data[0][0], shop_data = res.data.data[1], _shop_data = []; 
            var shop_current = Number(String(delivery_data.driver_status).replace("shop_", ""))-1 || 0;
            if (delivery_data.driver_status == 'end'){
              shop_current = shop_data.length;
            }
            that.setData({
              delivery_data: delivery_data,
              total_mileage: (delivery_data.total_mileage / 1000).toFixed(0),
              total_duration: (delivery_data.total_duration / 60).toFixed(2),
              shop_number: shop_data.length,
              al_shop_number: (shop_current < 0 ? 0 : shop_current)
            });
            if(e.pl){
              for (var i = 0; i < shop_data.length; i++) {
                if (shop_data[i].DID == e.pl){
                  _shop_data.push(shop_data[i]);
                }
              };
              that.setData({
                shop_data: _shop_data
              })
            }else{
              that.setData({
                shop_data: shop_data,
              })
            };
          }else{
            that.setData({
              hasData:false
            })
          }
        },
        fail:function(error){
          wx.hideToast();
          console.log(error);
          that.setData({
              hasData:false
            })
        }
      })
  },
  callSomeOne:function(e){
    wx.makePhoneCall({
      phoneNumber: e.target.id
    })
  }
})
